package com.jinlia.show.modules.wxapp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 腾讯地图
 * https://lbs.qq.com/dev/console/application/mine
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ten/xun")
public class TenXunApi {
    @Autowired
    private RestTemplate restTemplate;

    String url = "https://apis.map.qq.com/ws/geocoder/v1/?location=%S,%S&key=SNGBZ-KENCI-ARJGU-UMNP5-ZT2TJ-RKFI4&get_poi=1";

    /**
     * 地点搜索
     * https://lbs.qq.com/service/webService/webServiceGuide/search/webServiceSearch
     */
    String placeSearchUrl = "https://apis.map.qq.com/ws/place/v1/search?boundary=nearby(%S,%S,1000)&keyword=%S&page_size=10&page_index=1&key=SNGBZ-KENCI-ARJGU-UMNP5-ZT2TJ-RKFI4";
    /**
     * 通过经纬度解析地址
     *
     * @param param
     * @return
     */
    @PostMapping("geocoder")
    public JSONObject geocoder(@RequestBody Map<String, String> param) {
        String forObject = restTemplate.getForObject(String.format(url, param.get("latitude"), param.get("longitude")), String.class);
        JSONObject jsonObject = JSON.parseObject(forObject);
        return jsonObject;
    }

    @PostMapping("search")
    public JSONObject search(@RequestBody Map<String, String> param) {
        String forObject = restTemplate.getForObject(String.format(placeSearchUrl, param.get("latitude"), param.get("longitude"),param.get("keyWords")), String.class);
        JSONObject jsonObject = JSON.parseObject(forObject);
        return jsonObject;
    }
}
